home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gsfont.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  9KB  |  307 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsfont.c */
  20. /* Font operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gzstate.h"            /* must precede gxdevice */
  27. #include "gxdevice.h"            /* must precede gxfont */
  28. #include "gschar.h"
  29. #include "gxfont.h"
  30. #include "gxfdir.h"
  31.  
  32. /* Imported procedures */
  33. void    gs_purge_font_from_char_caches(P2(gs_font_dir *, const gs_font *));
  34.  
  35. /* Size of cache structures */
  36. extern const uint cached_char_sizeof;
  37. extern const uint cached_fm_pair_sizeof;
  38.  
  39. /* Define the sizes of the various aspects of the font/character cache. */
  40. /*** Big memory machines ***/
  41. #define smax_LARGE 50        /* smax - # of scaled fonts */
  42. #define bmax_LARGE 500000    /* bmax - space for cached chars */
  43. #define mmax_LARGE 200        /* mmax - # of cached font/matrix pairs */
  44. #define cmax_LARGE 5000        /* cmax - # of cached chars */
  45. #define blimit_LARGE 2500    /* blimit/upper - max size of a single cached char */
  46. /*** Small memory machines ***/
  47. #define smax_SMALL 20        /* smax - # of scaled fonts */
  48. #define bmax_SMALL 25000    /* bmax - space for cached chars */
  49. #define mmax_SMALL 40        /* mmax - # of cached font/matrix pairs */
  50. #define cmax_SMALL 500        /* cmax - # of cached chars */
  51. #define blimit_SMALL 100    /* blimit/upper - max size of a single cached char */
  52.  
  53. /* Allocate a font directory */
  54. gs_font_dir *
  55. gs_font_dir_alloc(const gs_memory_procs *mprocs)
  56. {    /* Try allocating a very large cache. */
  57.     /* If this fails, allocate a small one. */
  58. #if !arch_ints_are_short
  59.     gs_font_dir *pdir;
  60.     pdir = gs_font_dir_alloc_limits(mprocs,
  61.                     smax_LARGE, bmax_LARGE, mmax_LARGE,
  62.                     cmax_LARGE, blimit_LARGE);
  63.     if ( pdir != 0 ) return pdir;
  64. #endif
  65.     return gs_font_dir_alloc_limits(mprocs,
  66.                     smax_SMALL, bmax_SMALL, mmax_SMALL,
  67.                     cmax_SMALL, blimit_SMALL);
  68. }
  69. gs_font_dir *
  70. gs_font_dir_alloc_limits(const gs_memory_procs *mprocs,
  71.   uint smax, uint bmax, uint mmax, uint cmax, uint upper)
  72. {    register gs_font_dir *pdir = (gs_font_dir *)(*mprocs->alloc)(1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  73.     uint chsize = (cmax / 5) | 31;        /* a guess */
  74.     cached_fm_pair *mdata;
  75.     cached_char **chars;
  76.     if ( pdir == 0 ) return 0;
  77.     /* Round up chsize to a power of 2. */
  78.     while ( chsize & (chsize + 1) ) chsize |= chsize >> 1;
  79.     chsize++;
  80.     mdata = (cached_fm_pair *)(*mprocs->alloc)(mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  81.     chars = (cached_char **)(*mprocs->alloc)(chsize, sizeof(cached_char *), "font_dir_alloc(chars)");
  82.     if ( mdata == 0 || chars == 0 )
  83.        {    if ( chars != 0 ) (*mprocs->free)((char *)chars, chsize, sizeof(cached_char *), "font_dir_alloc(chars)");
  84.         if ( mdata != 0 ) (*mprocs->free)((char *)mdata, mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  85.         (*mprocs->free)((char *)pdir, 1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  86.         return 0;
  87.        }
  88.     memset((char *)pdir, 0, sizeof(gs_font_dir));    /* easiest to clear everything first */
  89.     pdir->mprocs = mprocs;
  90.     pdir->smax = smax;
  91.     pdir->fmcache.mmax = mmax;
  92.     pdir->fmcache.mdata = mdata;
  93.     pdir->ccache.mprocs = mprocs;
  94.     pdir->ccache.bmax = bmax;
  95.     pdir->ccache.cmax = cmax;
  96.     pdir->ccache.lower = upper / 10;
  97.     pdir->ccache.upper = upper;
  98.     pdir->ccache.chars = chars;
  99.     pdir->ccache.chars_mask = chsize - 1;
  100.     gx_char_cache_init(pdir);
  101.     return pdir;
  102. }
  103.  
  104. /* Macro for linking an element at the head of a chain */
  105. #define link_first(first, elt)\
  106.   if ( (elt->next = first) != NULL ) first->prev = elt;\
  107.   elt->prev = 0;\
  108.   first = elt
  109.  
  110. /* definefont */
  111. /* Use this only for original (unscaled) fonts! */
  112. int
  113. gs_definefont(gs_font_dir *pdir, gs_font *pfont)
  114. {    link_first(pdir->orig_fonts, pfont);
  115.     pfont->dir = pdir;
  116.     pfont->base = pfont;
  117.     return 0;
  118. }
  119.  
  120. /* scalefont */
  121. int
  122. gs_scalefont(gs_font_dir *pdir, const gs_font *pfont, floatp scale,
  123.   gs_font **ppfont, gs_font **pdfont)
  124. {    gs_matrix mat;
  125.     gs_make_scaling(scale, scale, &mat);
  126.     return gs_makefont(pdir, pfont, &mat, ppfont, pdfont);
  127. }
  128.  
  129. /* makefont */
  130. int
  131. gs_makefont(gs_font_dir *pdir, const gs_font *pfont, const gs_matrix *pmat,
  132.   gs_font **ppfont, gs_font **pdfont)
  133. {    int code;
  134.     gs_font *prev = 0;
  135.     gs_font *pf_out = pdir->scaled_fonts;
  136.     gs_matrix newmat;
  137.     *pdfont = 0;
  138.     gs_make_identity(&newmat);    /* fill in tags */
  139.     if ( (code = gs_matrix_multiply(&pfont->FontMatrix, pmat, &newmat)) < 0 )
  140.       return code;
  141.     /* Check for the font already being in the scaled font cache. */
  142.     /* Only attempt to share fonts if the current font has */
  143.     /* a valid UniqueID or XUID. */
  144. #ifdef DEBUG
  145. if ( gs_debug['m'] )
  146.    {    if ( pfont->data.base.UID.size == 0 )    /* UniqueID */
  147.       dprintf1("[m]UniqueID=%ld", pfont->data.base.UID.u.id);
  148.     else
  149.       dprintf1("[m]XUID(%d)", pfont->data.base.UID.size);
  150.     dprintf7(", FontType=%d,\n[m]  new FontMatrix=[%g %g %g %g %g %g]\n",
  151.       pfont->FontType,
  152.       pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  153.       pmat->tx, pmat->ty);
  154.    }
  155. #endif
  156.     if ( uid_is_valid(&pfont->data.base.UID) )
  157.       for ( ; pf_out != 0; prev = pf_out, pf_out = pf_out->next )
  158.         if ( uid_equal(&pf_out->data.base.UID, &pfont->data.base.UID) &&
  159.          pf_out->base == pfont->base &&
  160.          pf_out->FontType == pfont->FontType &&
  161.          pf_out->FontMatrix.xx == newmat.xx &&
  162.          pf_out->FontMatrix.xy == newmat.xy &&
  163.          pf_out->FontMatrix.yx == newmat.yx &&
  164.          pf_out->FontMatrix.yy == newmat.yy &&
  165.          pf_out->FontMatrix.tx == newmat.tx &&
  166.          pf_out->FontMatrix.ty == newmat.ty
  167.            )
  168.         {    *ppfont = pf_out;
  169.             if_debug1('m', "[m]found font=%lx\n", (ulong)pf_out);
  170.             return 0;
  171.         }
  172.     pf_out = (gs_font *)(*pdir->mprocs->alloc)(1, sizeof(gs_font), "gs_makefont");
  173.     if ( !pf_out ) return_error(gs_error_VMerror);
  174.     *pf_out = *pfont;
  175.     pf_out->FontMatrix = newmat;
  176.     pf_out->client_data = 0;
  177.     if ( uid_is_valid(&pfont->data.base.UID) )
  178.     {    if ( pdir->ssize == pdir->smax )
  179.         {    /* Must discard a cached scaled font. */
  180.             /* prev points to the last (oldest) font. */
  181.             if_debug1('m', "[m]discarding font %lx\n",
  182.                   (ulong)prev);
  183.             *pdfont = prev;
  184.             prev->prev->next = 0;
  185.         }
  186.         else
  187.             pdir->ssize++;
  188.         link_first(pdir->scaled_fonts, pf_out);
  189.     }
  190.     pf_out->dir = pdir;
  191.     pf_out->base = pfont->base;
  192.     *ppfont = pf_out;
  193.     if_debug1('m', "[m]new font=%lx\n", (ulong)pf_out);
  194.     return 1;
  195. }
  196.  
  197. /* setfont */
  198. int
  199. gs_setfont(gs_state *pgs, gs_font *pfont)
  200. {    pgs->font = pfont;
  201.     pgs->char_tm_valid = 0;
  202.     return 0;
  203. }
  204.  
  205. /* currentfont */
  206. gs_font *
  207. gs_currentfont(const gs_state *pgs)
  208. {    return pgs->font;
  209. }
  210.  
  211. /* cachestatus */
  212. void
  213. gs_cachestatus(register const gs_font_dir *pdir, register uint pstat[7])
  214. {    pstat[0] = pdir->ccache.bsize;
  215.     pstat[1] = pdir->ccache.bmax;
  216.     pstat[2] = pdir->fmcache.msize;
  217.     pstat[3] = pdir->fmcache.mmax;
  218.     pstat[4] = pdir->ccache.csize;
  219.     pstat[5] = pdir->ccache.cmax;
  220.     pstat[6] = pdir->ccache.upper;
  221. }
  222.  
  223. /* setcachelimit */
  224. int
  225. gs_setcachelimit(gs_font_dir *pdir, uint size)
  226. {    pdir->ccache.upper = size;
  227.     return 0;
  228. }
  229.  
  230. /* setcacheparams */
  231. int
  232. gs_setcachelower(gs_font_dir *pdir, uint size)
  233. {    pdir->ccache.lower = size;
  234.     return 0;
  235. }
  236. int
  237. gs_setcacheupper(gs_font_dir *pdir, uint size)
  238. {    pdir->ccache.upper = size;
  239.     return 0;
  240. }
  241.  
  242. /* currentcacheparams */
  243. uint
  244. gs_currentcachelower(const gs_font_dir *pdir)
  245. {    return pdir->ccache.lower;
  246. }
  247. uint
  248. gs_currentcacheupper(const gs_font_dir *pdir)
  249. {    return pdir->ccache.upper;
  250. }
  251.  
  252. /* Dummy (ineffective) BuildChar/BuildGlyph procedure */
  253. int
  254. gs_no_build_char_proc(struct gs_show_enum_s *penum, gs_state *pgs,
  255.   gs_font *pfont, gs_char chr, gs_glyph glyph)
  256. {    return 1;            /* failure, but not error */
  257. }
  258.  
  259. /* Dummy character encoding procedure */
  260. gs_glyph
  261. gs_no_encode_char_proc(struct gs_show_enum_s *penum,
  262.   gs_font *pfont, gs_char *pchr)
  263. {    return gs_no_glyph;
  264. }
  265.  
  266. /* Purge a font from all font- and character-related tables. */
  267. /* This is only used by restore (and, someday, the GC). */
  268. void
  269. gs_purge_font(const gs_font *pfont)
  270. {    gs_font_dir *pdir = pfont->dir;
  271.     gs_font *pf;
  272.  
  273.     /* Remove the font from its list (orig_fonts or scaled_fonts). */
  274.     gs_font *prev = pfont->prev;
  275.     gs_font *next = pfont->next;
  276.     if ( next != 0 )
  277.         next->prev = prev;
  278.     if ( prev != 0 )
  279.         prev->next = next;
  280.     else if ( pdir->orig_fonts == pfont )
  281.         pdir->orig_fonts = next;
  282.     else if ( pdir->scaled_fonts == pfont )
  283.         pdir->scaled_fonts = next;
  284.     else
  285.         /* Shouldn't happen! */
  286.         ;
  287.     if ( pfont->base != pfont )
  288.     {    /* I.e., this is a scaled font. */
  289.         pdir->ssize--;
  290.     }
  291.  
  292.     /* Purge the font from the scaled font cache. */
  293.     for ( pf = pdir->scaled_fonts; pf != 0; )
  294.     {    if ( pf->base == pfont )
  295.         {    gs_purge_font(pf);
  296.             pf = pdir->scaled_fonts; /* start over */
  297.         }
  298.         else
  299.             pf = pf->next;
  300.     }
  301.  
  302.     /* Purge the font from the font/matrix pair cache, */
  303.     /* including all cached characters rendered with that font. */
  304.     gs_purge_font_from_char_caches(pdir, pfont);
  305.  
  306. }
  307.